library SideArms initializer init requires LegacySystem, AmmoAPI

	private function SideArmFire takes nothing returns nothing
		local unit u = GetTriggerUnit()
		local player p = GetOwningPlayer(u)
		local integer i = GetSpellAbilityId()
		local integer id = GetConvertedPlayerId(p)
		local integer current = udg_Second
		local boolean firing = false
		
		local integer pistolammo = getPlayerDataInteger(p,"pistol_ammo")

		//call print("SideArmFire:" + I2S(pistolammo))

		//===========================
		// Benelli
		//===========================
		if i=='A00S' then 
			if current-udg_BenelliLimit!=0 then
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\m3-4.wav"
				call PlaySoundPath3D.execute()
			else
				set udg_BenelliLimit = current
			endif
	   

		//===========================
		// Browning
		//===========================
		elseif i=='A00T' then 
			if current-udg_BrowningLimit!=0 then
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\xm1014-3.wav"
				call PlaySoundPath3D.execute()
			else
				set udg_BrowningLimit = current
			endif

		//===========================
		// Colt
		//===========================
		elseif i=='A06H' then 
			if (pistolammo>0) then
				set firing = true
				call setPlayerDataInteger(p,"pistol_ammo",pistolammo-1)
				set pistolammo = pistolammo -1
				if current-udg_ColtLimit!=0 then
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\usp_unsil-2.wav"
					call PlaySoundPath3D.execute()
				else
					set udg_ColtLimit = current
				endif
			endif

			if firing==true then
				call PolledWait(1.5)
			endif

			if pistolammo<=0 and GetUnitAbilityLevel(u,'A06H')>0 then
				call UnitRemoveAbility(u,'A06H')
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\usp_clipout.wav"
				call PlaySoundPath3D.execute()
				call PolledWait(4.)
				if GetPrimaryAmmoMagazines(p)>1 or (GetPrimaryAmmoMagazines(p)>0 and udg_Reloading[id]!=2 and udg_Reloading[id]!=1) then
					call setPlayerDataBoolean(p,"pistol_reload",true)
					call AddPrimaryAmmo(u, -1, 0)
					call ShiftMoveSpeed(udg_Hero[id],1.4)
					set current = udg_Second
					if current-udg_ClipInLimit!=0 then
						set bj_meleeNearestMineDist = GetUnitX(u)
						set bj_lastTransmissionDuration = GetUnitY(u)
						set udg_soundpath = "war3mapImported\\usp_clipin.wav"
						call PlaySoundPath3D.execute()
					else
						set udg_ClipInLimit=current
					endif
					call PolledWait(1.)
					call setPlayerDataInteger(p,"pistol_ammo",8)
					call UnitAddAbility(u,'A06H')
					if udg_Experience[id]>65000 then
						call IncUnitAbilityLevel(u,'A06H') //Colt
					endif
					call setPlayerDataBoolean(p,"pistol_reload",false)
				endif
			endif

		//===========================
		// Desert Eagle
		//===========================

		elseif i=='A07P' then 
			if pistolammo>0 then
				set firing = true
				call setPlayerDataInteger(p,"pistol_ammo",pistolammo-1)
				set pistolammo = pistolammo -1
				if current-udg_DEagleLimit!=0 then
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\deagle.wav"
					call PlaySoundPath3D.execute()
				else
					set udg_DEagleLimit = current
				endif
			endif

			if firing==true then
				call PolledWait(1.5)
			endif

			if pistolammo<=0 and GetUnitAbilityLevel(u,'A07P')>0 then
				call UnitRemoveAbility(u,'A07P')
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\usp_clipout.wav"
				call PlaySoundPath3D.execute()
				call PolledWait(4.)
				if GetPrimaryAmmoMagazines(p)>1 or (GetPrimaryAmmoMagazines(p)>0 and udg_Reloading[id]!=2 and udg_Reloading[id]!=1) then
					call setPlayerDataBoolean(p,"pistol_reload",true) 
					
					call AddPrimaryAmmo(u, -1, 0)
					call ShiftMoveSpeed(udg_Hero[id],1.4)
					set current = udg_Second
					if current-udg_ClipInLimit!=0 then
						set bj_meleeNearestMineDist = GetUnitX(u)
						set bj_lastTransmissionDuration = GetUnitY(u)
						set udg_soundpath = "war3mapImported\\usp_clipin.wav"
						call PlaySoundPath3D.execute()
					else
						set udg_ClipInLimit=current
					endif
					call PolledWait(1.)
					call setPlayerDataInteger(p,"pistol_ammo",8)
					call UnitAddAbility(u,'A07P')
					if udg_Experience[id]>65000 then
						call IncUnitAbilityLevel(u,'A07P') //Desert Eagle
					endif
					call setPlayerDataBoolean(p,"pistol_reload",false)
				endif
			endif

		//===========================
		// Beretta 
		//===========================
		elseif i=='A08T' then 

			if pistolammo>0 then
				set firing = true
				call setPlayerDataInteger(p,"pistol_ammo",pistolammo-1)
				set pistolammo = pistolammo -1
				if current-udg_DEagleLimit!=0 then
					set bj_meleeNearestMineDist = GetUnitX(u)
					set bj_lastTransmissionDuration = GetUnitY(u)
					set udg_soundpath = "war3mapImported\\usp_unsil-2.wav"
					call PlaySoundPath3D.execute()
				else
					set udg_ColtLimit = current
				endif
			endif

			if firing==true then
				call PolledWait(1.0)
			endif

			if pistolammo<=0 and GetUnitAbilityLevel(u,'A08T')>0 then
				call UnitRemoveAbility(u,'A08T')
				set bj_meleeNearestMineDist = GetUnitX(u)
				set bj_lastTransmissionDuration = GetUnitY(u)
				set udg_soundpath = "war3mapImported\\usp_clipout.wav"
				call PlaySoundPath3D.execute()
				call PolledWait(4.)
				
				if GetPrimaryAmmoMagazines(p)>1 or (GetPrimaryAmmoMagazines(p)>0 and udg_Reloading[id]!=2 and udg_Reloading[id]!=1) then
					call setPlayerDataBoolean(p,"pistol_reload",true) 
					call AddPrimaryAmmo(u, -1, 0)
					
					call ShiftMoveSpeed(udg_Hero[id],1.4)
					set current = udg_Second
					if current-udg_ClipInLimit!=0 then
						set bj_meleeNearestMineDist = GetUnitX(u)
						set bj_lastTransmissionDuration = GetUnitY(u)
						set udg_soundpath = "war3mapImported\\usp_clipin.wav"
						call PlaySoundPath3D.execute()
					else
						set udg_ClipInLimit=current
					endif
					call PolledWait(1.)
					call setPlayerDataInteger(p,"pistol_ammo",15)
					call UnitAddAbility(u,'A08T')
					if udg_Experience[id]>65000 then
						call IncUnitAbilityLevel(u,'A08T') //Desert Eagle
					endif
					call setPlayerDataBoolean(p,"pistol_reload",false)
				endif
			endif
		endif
		set u = null
	endfunction

	private function SideArmsCondition takes nothing returns boolean
		local integer i = GetSpellAbilityId()
		return i == 'A08T' or i == 'A07P' or i == 'A06H' or i == 'A00T' or i == 'A00S'
	endfunction

	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
		call TriggerAddCondition(t, Condition(function SideArmsCondition))
		call TriggerAddAction(t,function SideArmFire)
	endfunction



endlibrary